package com.org.any.autocar.fragment;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

import com.autocar.reader.R;
import com.autocar.reader.bean.ChatBean;
import com.autocar.reader.bean.RequestBean;
import com.autocar.reader.bean.XiaoxiBean;

public class DialogControllerAdd {

	private PopupWindow sharePw;
	private LayoutInflater inflater;
	private TextView contentEt;
	private IDialogController idialog;
	private LinearLayout mainLl;
	XiaoxiBean bean = null;
	private Context context = null;

	public DialogControllerAdd(Context context,XiaoxiBean bean) {
		this.inflater = LayoutInflater.from(context);
		this.context = context;
		this.bean = bean;
		initview();
	}

	public void setIDialogController(IDialogController idialog) {
		this.idialog = idialog;
	}

	private void initview() {
		if (mainLl == null) {
			mainLl = (LinearLayout) inflater.inflate(
					R.layout.request_add_ok_show_dialog, null);

			contentEt = (TextView) mainLl
					.findViewById(R.id.request_add_show_dialog_et_content);
			String content = String.format(context.getString(R.string.request_add_validation_content),bean.getUsername());
			contentEt.setText(content+"");
			Button okBtn = (Button) mainLl
					.findViewById(R.id.request_add_show_dialog_bt_ok);
			Button cancelBtn = (Button) mainLl
					.findViewById(R.id.request_add_show_dialog_bt_cancel);

			MyOnclick on = new MyOnclick();

			okBtn.setOnClickListener(on);
			cancelBtn.setOnClickListener(on);

			mainLl.getBackground().setAlpha(100);
		}
		if (sharePw == null) {
			sharePw = new PopupWindow(mainLl, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT, true);
			sharePw.setBackgroundDrawable(new BitmapDrawable());
		}
	}

	public void showPw(View view) {
		if (!sharePw.isShowing())
			sharePw.showAtLocation(view, Gravity.CENTER, 0, 0);

	}

	private String getContent() {
		return this.contentEt.getText().toString().trim();
	}

	class MyOnclick implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.request_add_show_dialog_bt_ok:
				if (idialog != null) {
					idialog.doOk(bean);
				}
				break;
			case R.id.request_add_show_dialog_bt_cancel:
				if (idialog != null) {
					idialog.doCancel(bean);
				}
				break;
			}
			if (sharePw.isShowing()) {
				sharePw.dismiss();
			}
		}
	}

	public interface IDialogController {
		public void doOk(XiaoxiBean bean);

		public void doCancel(XiaoxiBean bean);
	}

}
